package app.qrcode.browser.ykpixabay.ui.login

import android.os.Bundle
import android.text.Editable
import android.text.TextWatcher
import android.view.LayoutInflater
import android.view.View
import android.view.ViewGroup
import android.widget.Toast
import androidx.fragment.app.Fragment
import androidx.navigation.NavController
import androidx.navigation.Navigation
import app.qrcode.browser.ykpixabay.R
import cn.leancloud.LCUser
import io.reactivex.Observer
import io.reactivex.disposables.Disposable
import kotlinx.android.synthetic.main.fragment_signup.*

/**
 * Created by YiGo on 2022/4/21.
 */
class SignupFragment : Fragment() {

    override fun onCreateView(
        inflater: LayoutInflater,
        container: ViewGroup?,
        savedInstanceState: Bundle?
    ): View? {
        return inflater.inflate(R.layout.fragment_signup, container, false)
    }

    override fun onViewCreated(view: View, savedInstanceState: Bundle?) {
        super.onViewCreated(view, savedInstanceState)
        val controller: NavController = Navigation.findNavController(
            this.requireActivity(),
            R.id.fragmentContainerView
        )
        signup_confirm_btn.isEnabled = false
        edittext_signup_username.addTextChangedListener(watcher)
        edittext_signup_password.addTextChangedListener(watcher)
        signup_progressBar.visibility = View.INVISIBLE
        signup_confirm_btn.setOnClickListener {
            val name = edittext_signup_username.text?.trim().toString()
            val pwd = edittext_signup_password.text?.trim().toString()
            LCUser().also {
                signup_progressBar.visibility = View.VISIBLE
                it.username = name
                it.password = pwd
                it.signUpInBackground().subscribe(object : Observer<LCUser> {
                    override fun onSubscribe(d: Disposable) {
                    }

                    override fun onNext(t: LCUser) {
                        Toast.makeText(context, "注册成功", Toast.LENGTH_SHORT).show()
                        LCUser.logIn(name, pwd).subscribe(object : Observer<LCUser> {
                            override fun onSubscribe(d: Disposable) {
                            }

                            override fun onNext(t: LCUser) {
                                //跳转到主界面
                                controller.navigate(R.id.action_signupFragment_to_viewPicFragment)
                            }

                            override fun onError(e: Throwable) {
                                signup_progressBar.visibility = View.INVISIBLE
                                Toast.makeText(context, "${e.message}", Toast.LENGTH_SHORT).show()
                            }

                            override fun onComplete() {
                            }

                        })
                    }

                    override fun onError(e: Throwable) {
                        Toast.makeText(context, "${e.message}", Toast.LENGTH_SHORT).show()
                        signup_progressBar.visibility = View.INVISIBLE
                    }

                    override fun onComplete() {
                    }

                })
            }
        }
    }

    private val watcher = object : TextWatcher {
        override fun beforeTextChanged(p0: CharSequence?, p1: Int, p2: Int, p3: Int) {

        }

        override fun onTextChanged(p0: CharSequence?, p1: Int, p2: Int, p3: Int) {
            val t1 = edittext_signup_username.text.toString().isNotEmpty()
            val t2 = edittext_signup_password.text.toString().isNotEmpty()
            signup_confirm_btn.isEnabled = t1 and t2
        }

        override fun afterTextChanged(p0: Editable?) {

        }
    }

}